package com.ujiuye.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ujiuye.mapper.OrdersettingMapper;
import com.ujiuye.pojo.Ordersetting;
import com.ujiuye.service.IOrdersettingService;
import com.ujiuye.utils.MessageConstant;
import com.ujiuye.utils.Result;
import com.ujiuye.vo.DateVo;
import org.apache.dubbo.config.annotation.Service;


import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会员设置-比如设置今天可预约人数等信息 服务实现类
 * </p>
 *
 * @author zhangwenping
 * @since 2021-07-05
 */
@Service
public class OrdersettingServiceImpl extends ServiceImpl<OrdersettingMapper, Ordersetting> implements IOrdersettingService {

    @Resource
    private OrdersettingMapper ordersettingMapper;

    @Override
    public Result update(List<Ordersetting> list) {
        //保存错误日期
        List<String> errorDateList = new ArrayList<>();
        //遍历list,
        for (Ordersetting ordersetting : list) {
            //根据excel转化为list集合数据的日期，查询数据
            QueryWrapper<Ordersetting> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("orderDate",ordersetting.getOrderdate());
            List<Ordersetting> ordersettingList = ordersettingMapper.selectList(queryWrapper);
            //判断是否有数据，有数据修改，没数据添加
            if (ordersettingList!=null && ordersettingList.size()>0){//日期在数据库存在，修改
                //获取数据库的对象
                Ordersetting sysOrdersetting = ordersettingList.get(0);
                //判断数据库的已预约人数是否小于文件的可预约人数
                if (sysOrdersetting.getReservations()<ordersetting.getNumber()){//可以修改可预约人数
                    sysOrdersetting.setNumber(ordersetting.getNumber());//修改数据库可预约人数
                    ordersettingMapper.updateById(ordersetting);
                }else {
                    //已预约人数大于文件的可预约人数，日期不符合修改条件
                    String errorDate = localDateToString(ordersetting.getOrderdate());
                    errorDateList.add(errorDate);
                }
            }
            else {
                //日期在数据库不存在，添加
                ordersettingMapper.insert(ordersetting);
            }
        }
        //如果错误日期集合为空，说明全部添加成功
        if (errorDateList.size()==0){
            return new Result(true, MessageConstant.ORDER_SUCCESS);
        }
        //如果错误日期集合不为空，说明部分添加成功
        return new Result(false, "部分设置成功，日期"+errorDateList+"设置失败，可预约人数不能小于已预约人数");
    }

    @Override
    public List<DateVo> listDate(String datetime) {
        List<DateVo> dateVoList = new ArrayList<>();
        String beginTime = datetime+"-01";
        String endTime = datetime+"-31";
        QueryWrapper<Ordersetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("orderDate",beginTime,endTime);
        //查询一个月的日期数据
        List<Ordersetting> ordersettingList = ordersettingMapper.selectList(queryWrapper);
        //判断是否为空
        if (ordersettingList!=null && ordersettingList.size()>0){
            for (Ordersetting ordersetting : ordersettingList) {
                //遍历，将ordersetting的数据传入DateVo中
                DateVo dateVo = new DateVo(ordersetting.getOrderdate().getDayOfMonth(),
                        ordersetting.getNumber(),ordersetting.getReservations());
                //将dateVo添加到dateVoList集合中
                dateVoList.add(dateVo);
            }
        }
        return dateVoList;
    }

    @Override
    public Result update(Date date, Integer number) {
        //将date转localDate
        LocalDate localDate = date2LocalDate(date);
        //根据localDate查询数据库
        QueryWrapper<Ordersetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderDate", localDate);
        //判断时间是否已经设置
        List<Ordersetting> ordersettingList = ordersettingMapper.selectList(queryWrapper);
        //判断ordersettingList是否为空，为空添加，不为空修改
        if (ordersettingList!=null && ordersettingList.size()>0){//修改
            Ordersetting ordersetting = ordersettingList.get(0);
            //判断数据库的已预约人数是否小于可预约人数，小于可以改，大于不给该
            if (ordersetting.getReservations()>number){//不可以修改可预约人数
                return new Result(false,"设置失败，已预约人数大于可预约人数");
            }
            ordersetting.setNumber(number);//修改数据库可预约人数
            ordersettingMapper.updateById(ordersetting);
        }else {//将ordersetting添加到数据库
            Ordersetting ordersetting = new Ordersetting(null,localDate,number,0);
            ordersettingMapper.insert(ordersetting);
        }
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }

    //localDate转String
    public String localDateToString(LocalDate localDate){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return localDate.format(dateTimeFormatter);
    }

    /**
     * Date转LocalDate
     * @param date
     */
    public static LocalDate date2LocalDate(Date date) {
        if(null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }


}
